/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ch.repit.aglaja.client.contacts;

import ch.repit.rwt.client.AttributeType;
import ch.repit.rwt.client.Bento;
import ch.repit.rwt.client.BentoAttribute;
import ch.repit.rwt.client.BentoDef;
import ch.repit.rwt.client.BentoDefFactory;
import ch.repit.rwt.client.ObjectRef;
import ch.repit.rwt.client.logs.LogManager;
import ch.repit.rwt.client.persistence.CacheManager;
import ch.repit.rwt.client.ui.form.FormPage;
import com.google.gwt.user.client.ui.TextBox;

/**
 * Page dedicated to the creation of a new contact with a new address. This allows
 * to have in the same form fields from 2 distinct objects, but correlated as in many cases
 * an address will be owned by a single person.
 */
class NewAddressPage extends FormPage {


    private ObjectRef personRef;

    private TextBox firstname;
    private TextBox lastname;

    /**
     * The personRef is null in most cases. If not null, the person will be assigned the newly created address
     * @param userRef
     */
    public NewAddressPage(ObjectRef personRef) 
    {
        super(AddressDef.TYPE, null, null);
        this.personRef = personRef;

        // here we need to have fields that do not belong to the current object, but that will be used
        // after the save, to create a user.
        if (personRef==null)
            setTitle("Saisie d'un nouveau contact");
        else
            setTitle("Déménagement vers une nouvelle adresse");

        addSectionHead("Personne");
        addWidgetRow("Prénom", null, firstname = new TextBox());
        addWidgetRow("Nom de famille", null, lastname = new TextBox());

        addSectionHead("Regroupement");
        if (personRef != null) {
            Bento person = CacheManager.get().getCachedObject(personRef);
            if (person != null) {
                firstname.setText(person.getDisplayValue(PersonDef.ATTR_FIRSTNAME));
                firstname.setEnabled(false);
                lastname.setText(person.getDisplayValue(PersonDef.ATTR_LASTNAME));
                lastname.setEnabled(false);
            }
        }

        // insert the standard fields
        AddressTab.staticInit(this);
    }


    
    @Override
    protected void doSaveOnSuccess(ObjectRef newAddressRef) {

        //  create (or update) the person.
        if (newAddressRef!=null && newAddressRef.getType().equals(AddressDef.TYPE)) {
            BentoDef personDef = BentoDefFactory.get().getDef(PersonDef.TYPE);
            Bento person;
            if (personRef == null) {
                person = personDef.createBento();
                BentoAttribute fna = new BentoAttribute(PersonDef.ATTR_FIRSTNAME, AttributeType.STRING);
                fna.set(firstname.getText());
                person.put(PersonDef.ATTR_FIRSTNAME, fna);
                BentoAttribute lna = new BentoAttribute(PersonDef.ATTR_LASTNAME, AttributeType.STRING);
                lna.set(lastname.getText());
                person.put(PersonDef.ATTR_LASTNAME, lna);
            } else
                person = CacheManager.get().getCachedObject(personRef);
            BentoAttribute addAttr = new BentoAttribute(PersonDef.ATTR_ADDRESSREF, AttributeType.STRING);
            addAttr.set(newAddressRef.toString());
            person.put(PersonDef.ATTR_ADDRESSREF, addAttr);
            this.doSave(person);
        }
        
        // and now do what we should
       // super.doSaveOnSuccess(newAddressRef);
        LogManager.get().handled("Enregistrement terminé.");
        CacheManager.get().refreshCache();
        getPageNav().back();
        getPageNav().displayPage(new ContactDetailsPage(newAddressRef));
    }




}
